/*
 * This file is part of the Sx Framework Library.
 * 
 * Copyright (C) 2013 University of Colorado Denver
 * <min.choi@ucdenver.edu> <shane.transue@ucdenver.edu>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy 
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
 * copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 * DEALINGS IN THE SOFTWARE.
 */
#ifndef SX_GRAPHICS_ARCHETYPE_H
#define SX_GRAPHICS_ARCHETYPE_H

#include <sxGraphicsObjectRenderer.h>

namespace Sx {
namespace Graphics {
namespace Archetypes {

/*
 * An archetype is defined as an original model that the definition of similar
 * objects are derived from (ex. a prototype) and are composed from the pure 
 * forms of other abstract objects to form an aggregate. The graphics archetype
 * constrains three independent concepts: the definition of an abstract object,
 * the definition of the renderer responsible for displaying that object, and
 * a physical representation that represents the physical attributes of the
 * object and how it exists in space (note that the Real type is defined for
 * the level of precision that should be used: ex float, double, etc).
 *
 * The GraphicsArchetype defines the ideal interface between an object, its
 * physical representation, and how it should be rendered (not that rendererd
 * in this context is not limited to being drawn on a screen, a renderer
 * implementation can do anything with the objects definition and the physical
 * representation). The archetype simply exposes the IDEAL interface for the
 * aggregate defined by the provided types.
 *
 * Important Note: When defining a renderer it must be tied to the ObjectType.
 * See: GraphicsObjectRenderer.
 */
template <typename ObjectType, typename Renderer, typename Real, typename PhysicalRepresentationType>
class GraphicsArchetype {
public:
	GraphicsArchetype();
	GraphicsArchetype(const std::shared_ptr<ObjectType>& object, const std::shared_ptr<Renderer>& renderer, const std::shared_ptr<PhysicalRepresentationType>& physicalRepresentation);
	virtual ~GraphicsArchetype();

	bool setObject(std::shared_ptr<ObjectType> object);
	bool setRenderer(std::shared_ptr<Renderer> renderer);
	bool setPhysicalRepresentation(std::shared_ptr<PhysicalRepresentationType> physicalRepresentation);

	void render() const;

	/*
	 * This construct function is responsible for constructing each of the 
	 * internal components in this archetype. The order of construction is as
	 * follows:
	 *   (1) Object                         Abstract Representation
	 *   (2) Physical Representation        Depends on Object
	 *   (3) Renderer                       Depends on Object and Physical Rep.
	 */
	bool construct();
	void update(Real dt) const;

	std::shared_ptr<ObjectType> const getObject() const;
	std::shared_ptr<Renderer> const getRenderer() const;
	std::shared_ptr<PhysicalRepresentationType> const getPhysicalRepresentation() const;

protected:
	std::shared_ptr<ObjectType> object;
	std::shared_ptr<Renderer> renderer;
	std::shared_ptr<PhysicalRepresentationType> physicalRepresentation;
};

}

}

}

template <typename ObjectType, typename Renderer, typename Real, typename PhysicalRepresentationType>
Sx::Graphics::Archetypes::GraphicsArchetype<ObjectType, Renderer, Real, PhysicalRepresentationType>::GraphicsArchetype() {}

template <typename ObjectType, typename Renderer, typename Real, typename PhysicalRepresentationType>
Sx::Graphics::Archetypes::GraphicsArchetype<ObjectType, Renderer, Real, PhysicalRepresentationType>::GraphicsArchetype(const std::shared_ptr<ObjectType>& object, const std::shared_ptr<Renderer>& renderer, const std::shared_ptr<PhysicalRepresentationType>& physicalRepresentation) {
	this->object = object;
	this->renderer = renderer;
	this->physicalRepresentation = physicalRepresentation;
}

template <typename ObjectType, typename Renderer, typename Real, typename PhysicalRepresentationType>
Sx::Graphics::Archetypes::GraphicsArchetype<ObjectType, Renderer, Real, PhysicalRepresentationType>::~GraphicsArchetype() {}

template <typename ObjectType, typename Renderer, typename Real, typename PhysicalRepresentationType>
bool Sx::Graphics::Archetypes::GraphicsArchetype<ObjectType, Renderer, Real, PhysicalRepresentationType>::setObject(std::shared_ptr<ObjectType> object) {
	if ( object == nullptr ) {
		std::cerr << "[GraphicsArcheType:setObject] Error: Provided object cannot be a nullptr." << std::endl;
		std::cerr << "  Archetype object representation not set." << std::endl;
		return false;
	}

	this->object = object;
	return true;
}

template <typename ObjectType, typename Renderer, typename Real, typename PhysicalRepresentationType>
bool Sx::Graphics::Archetypes::GraphicsArchetype<ObjectType, Renderer, Real, PhysicalRepresentationType>::setRenderer(std::shared_ptr<Renderer> renderer) {
	if ( renderer == nullptr ) {
		std::cerr << "[GraphicsArcheType:setRenderer] Error: Provided renderer cannot be a nullptr." << std::endl;
		std::cerr << "  Archetype renderer representation not set." << std::endl;
		return false;
	}

	this->renderer = renderer;
	return true;
}

template <typename ObjectType, typename Renderer, typename Real, typename PhysicalRepresentationType>
bool Sx::Graphics::Archetypes::GraphicsArchetype<ObjectType, Renderer, Real, PhysicalRepresentationType>::setPhysicalRepresentation(std::shared_ptr<PhysicalRepresentationType> physicalRepresentation) {
	if ( physicalRepresentation == nullptr ) {
		std::cerr << "[GraphicsArcheType:setPysicalRepresentation] Error: Provided physical representation cannot be a nullptr." << std::endl;
		std::cerr << "  Archetype physical representation not set." << std::endl;
		return false;
	}

	this->physicalRepresentation = physicalRepresentation;
	return true;
}

template <typename ObjectType, typename Renderer, typename Real, typename PhysicalRepresentationType>
bool Sx::Graphics::Archetypes::GraphicsArchetype<ObjectType, Renderer, Real, PhysicalRepresentationType>::construct() {
	bool success = false;

	if ( this->object == nullptr ) {
		std::cerr << "[GraphicsArcheType:construct] Error: Internal object is a nullptr." << std::endl;
		return false;
	}
	else success = this->object->construct();
	if ( !success ) return false;

	if ( this->physicalRepresentation == nullptr ) {
		std::cerr << "[GraphicsArcheType:construct] Error: Internal physical representation is a nullptr." << std::endl;
		return false;
	}
	else success = this->physicalRepresentation->construct();
	if ( !success ) return false;

	if ( this->renderer == nullptr ) {
		std::cerr << "[GraphicsArcheType:construct] Error: Internal renderer is a nullptr." << std::endl;
		return false;
	}
	else success = this->renderer->callConstruct(this->object.get(), this->physicalRepresentation.get());
	if ( !success ) return false;

	return true;
}

template <typename ObjectType, typename Renderer, typename Real, typename PhysicalRepresentationType>
void Sx::Graphics::Archetypes::GraphicsArchetype<ObjectType, Renderer, Real, PhysicalRepresentationType>::update(Real dt) const {
	//--------------------------------------------------------------------------
	// Graphics objects generally only need floating point precision.
	//--------------------------------------------------------------------------
	if ( this->object == nullptr ) return;
	else this->object->update(static_cast<float>(dt));

	if ( this->physicalRepresentation == nullptr ) return;
	else this->physicalRepresentation->update(dt);
	
	//--------------------------------------------------------------------------
	// Graphics renderers generally only need floating point precision.
	//--------------------------------------------------------------------------
	if ( this->renderer == nullptr ) return;
	else this->renderer->callUpdate(this->object.get(), static_cast<float>(dt));
}

template <typename ObjectType, typename Renderer, typename Real, typename PhysicalRepresentationType>
void Sx::Graphics::Archetypes::GraphicsArchetype<ObjectType, Renderer, Real, PhysicalRepresentationType>::render() const {
	if ( this->renderer == nullptr ) return;
	else this->renderer->callRender(this->object.get(), this->physicalRepresentation.get());
}

template <typename ObjectType, typename Renderer, typename Real, typename PhysicalRepresentationType>
std::shared_ptr<ObjectType> const Sx::Graphics::Archetypes::GraphicsArchetype<ObjectType, Renderer, Real, PhysicalRepresentationType>::getObject() const {
	return this->object;
}

template <typename ObjectType, typename Renderer, typename Real, typename PhysicalRepresentationType>
std::shared_ptr<Renderer> const Sx::Graphics::Archetypes::GraphicsArchetype<ObjectType, Renderer, Real, PhysicalRepresentationType>::getRenderer() const {
	return this->renderer;
}

template <typename ObjectType, typename Renderer, typename Real, typename PhysicalRepresentationType>
std::shared_ptr<PhysicalRepresentationType> const Sx::Graphics::Archetypes::GraphicsArchetype<ObjectType, Renderer, Real, PhysicalRepresentationType>::getPhysicalRepresentation() const {
	return this->physicalRepresentation;
}

#endif
